package com.gather.treesets;

import com.gather.orm.Teacher;

import java.util.Comparator;
import java.util.TreeSet;

public class Example03 {
    public static void main(String[] args) {
        System.out.println("使用Comparator");
        TreeSet<Teacher> teachers = new TreeSet<>(new Comparator<Teacher>() {
            @Override
            public int compare(Teacher o1, Teacher o2) {
                // 进行比较 先比较年龄再比较姓名 小到大排序
                int n = o1.getAge() - o2.getAge();
                int i = o1.getName().compareTo(o2.getName());
                return (n == 0) ? i : n;
            }
        });
        Teacher jack = new Teacher("jack", 20);
        Teacher kerry = new Teacher("kerry", 35);
        Teacher tom = new Teacher("tom", 30);
        teachers.add(jack);
        teachers.add(kerry);
        teachers.add(tom);
        /**
         * teachers [name:jack:20, name:tom:30, name:kerry:35]
         */
        System.out.println("teachers "+teachers);
    }
}
